package hu.dobrosi.j2me.ui;

import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class CustomFontTextCanvas extends Canvas {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8237311564792511089L;
	private static Image charsImage;
	private static String charTable;
	private static int charHeight;

	private static boolean charPixels[][][];

	int backgroundColor = 0x222222;
	int foregroundColor = 0xDDDDDD;	
	private int xOffset = 0, yOffset = 0;
	private String text = "";

	static {
		fillCharPixels();
	}

	public CustomFontTextCanvas() {
		super();
	}

	private static void fillCharPixels() {
		charTable = "@abcdefghijklmnopqrstuvwxyz[£]\\\\ !\"#$%&'()*+,-./0123456789:;<=>?\\ABCDEFGHIJKLMNOPQRSTUVWXYZ\\\n";
		charPixels = new boolean[charTable.length()][][];
		int[] charTopLeft = new int[charTable.length()];

		try {
			charsImage = Image.createImage(Image.class
					.getResourceAsStream("/chars.gif"));
			charHeight = 8;
		} catch (IOException e) {
			e.printStackTrace();
		}

		for (int i = 0; i < charTopLeft.length; i++) {
			charTopLeft[i] = i * 8;
		}

		for (int i = 0; i < charPixels.length - 1; i++) {
			int charWidth = charTopLeft[i + 1] - charTopLeft[i];
			charPixels[i] = new boolean[charHeight][charWidth];
			for (int y = 0; y < charHeight; y++) {
				for (int x = 0; x < charWidth; x++) {
					int[] tmp = new int[1];
					charsImage.getRGB(tmp, 0, 1, i * 8 + x, y, 1, 1);
					boolean v = tmp[0] != -1;
					charPixels[i][y][x] = v;
				}
			}
		}
	}

	public void paint(Graphics g) {
		xOffset = 0;
		yOffset = 0;
		g.setColor(backgroundColor);
		g.setColor(foregroundColor);
		g.fillRect(0, 0, getWidth(), getHeight());
		drawText(g, text);
	}

	private void drawText(Graphics g, String text) {
		for (int i = 0; i < text.length(); i++) {
			drawChar(g, text.charAt(i));
		}
	}

	private void drawChar(Graphics g, int c) {
		if (c == '\n') {
			xOffset = 0;
			yOffset += charHeight;
			return;
		}
		if ((c = charTable.indexOf(c)) == -1) {
			return;
		}
		boolean[][] charPixel = charPixels[c];
		int charWidth = 0;
		for (int y = 0; y < charHeight; y++) {
			charWidth = charPixel[y].length;
			if (xOffset + charWidth > getWidth()) {
				xOffset = 0;
				yOffset += charHeight;

			}
			for (int x = 0; x < charWidth; x++) {
				if (charPixel[y][x]) {
					g.setColor(0x0);
					g.drawLine(x + xOffset, y + yOffset, x + xOffset + 1, y
							+ yOffset);
				}
			}
		}
		xOffset += charWidth;
	}

	public void append(String text) {
		if (text.length() == 1) {
			int c = text.charAt(0);
			if (c == 8 && this.text.length() > 0) { // backspace
				setText(this.text.substring(0, this.text.length() - 1));
				return;
			} else if (charTable.indexOf(c) == -1) {
				return;
			}
		}
		setText(this.text + text);
	}

	public void setText(String text) {
		xOffset = 0;
		yOffset = 0;
		this.text = text;
		repaint();
	}

	/**
	 * @return the backgroundColor
	 */
	public int getBackgroundColor() {
		return backgroundColor;
	}

	/**
	 * @param backgroundColor the backgroundColor to set
	 */
	public void setBackgroundColor(int backgroundColor) {
		this.backgroundColor = backgroundColor;
	}

	/**
	 * @return the foregroundColor
	 */
	public int getForegroundColor() {
		return foregroundColor;
	}

	/**
	 * @param foregroundColor the foregroundColor to set
	 */
	public void setForegroundColor(int foregroundColor) {
		this.foregroundColor = foregroundColor;
	}
}
